package com.hlz.flink.chapter06;

import org.apache.commons.lang3.StringUtils;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.SlidingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;

import java.time.Duration;

/**
 * @author Hongliang Zhu
 * @create 2023-06-14 23:34
 */
public class WaterMarkerDemo {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        DataStreamSource<String> dataStreamSource = env.socketTextStream("127.0.0.1", 9696);
        SingleOutputStreamOperator<Stock> stockStream = dataStreamSource.filter(StringUtils::isNotEmpty)
                .map(new MapFunction<String, Stock>() {
                    @Override
                    public Stock map(String value) throws Exception {
                        final String[] split = value.split(",");
                        return new Stock(split[0], Double.parseDouble(split[1]), split[2], Long.parseLong(split[3]));
                    }
                });

        // 定義一個WaterMarkerStrategy
        WatermarkStrategy<Stock> stockWatermarkStrategy = WatermarkStrategy.<Stock>forBoundedOutOfOrderness(Duration.ofMillis(2)).withTimestampAssigner(new SerializableTimestampAssigner<Stock>() {
            @Override
            public long extractTimestamp(Stock element, long recordTimestamp) {
                return element.getTimeStamp();
            }
        });

        SingleOutputStreamOperator<Stock> stockSingleOutputStreamOperator = stockStream.assignTimestampsAndWatermarks(stockWatermarkStrategy);
        stockSingleOutputStreamOperator.keyBy(value -> value.getId())
                // 开窗分组
        .window(TumblingEventTimeWindows.of(Time.milliseconds(10)))
//                .allowedLateness(Time.milliseconds(5))
                .maxBy("price")
                .print("stockStream");


        env.execute("WatermarkDemo");


        // input

        /**

          stock_001,1,UDFStock,1
          stock_002,8,UDFStock,8
         stock_003,10,UDFStock,10
         stock_003,12,UDFStock,12
         stock_007,7,UDFStock,7
         stock_004,22,UDFStock,22
         stock_003,8,UDFStock,10
         stock_003,8,UDFStock,10


         */


    }
}
